-- DungeonSkyCombatM
-- Create by chendh
-- 地牢天空战

module("DungeonSkyCombatM", package.seeall);

local whenStartSkyCombat = nil;

-- 模块初始化
function init()
    EventMgr.removeAll("DungeonSkyCombatM");
    EventMgr.register("DungeonSkyCombatM", event.DUNGEON_SKY_COMBAT_START, whenStartSkyCombat);
end

whenStartSkyCombat = function(para)
    -- 翻开所有石板
    for pos = 1, GRID_SIZE do
        local grid = DungeonM.getGridByPos(pos);
        if not grid:isOpened() then
            grid:open();
        end
    end
end

-- 飞艇控制台行为
function airshipConsole(pos)
    -- 如果不是在迷宫中，则返回false
    if not DungeonM.isInDungeon() then
        return false;
    end

    -- 如果不是飞艇控制台，则返回false
    local grid = DungeonM.getGridByPos(pos);
    if type(grid) ~= "table" or grid.type ~= GRID_TYPE_AIRSHIP_CONSOLE then
        return false;
    end

    -- 加入飞艇控制台行为
    DungeonActionM.go("airship_console", pos);

    -- 抛出事件
    EventMgr.fire(event.DUNGEON_SKY_COMBAT_START, {});

    -- 保存下当前的地牢数据
    DungeonM.addAction({ ["cmd"] = "save_dungeon", });

    -- 开始迷宫同步
    DungeonM.sync();

    -- 切换状态
    -- DungeonM.switchState(DUNGEON_GAMING);
end

-- 服务器下发了战斗结果
function notifyCombatResult(result, costHp)
    local attackerInfo = result.attacker;
    local defenserInfo = result.defenser;
    local record = result.record;
    local bonus = result.bonus;

    -- 创建一个战场
    local dungeonId = defenserInfo["dungeon_id"];
    local layer = defenserInfo["layer"];
    local defenserName = string.format(getLocStr("dungeon_defenser_name"), DungeonAreaM.query(dungeonId, "name"), layer);
    defenserInfo["name"] = defenserName;

    local battle = SkyCombatM.createBattle(attackerInfo, defenserInfo, result.result, record, bonus, result.defenser_list);

    -- 抛出事件
    EventMgr.fire(event.DUNGEON_SKY_COMBAT_RESULT, {
        ["battle"] = battle,
        ["costHp"] = costHp,
    });

    -- 执行扣血
    local pos_list = result.pos_list or {};
    local kill_list = {};
    for _, pos in ipairs(pos_list) do
    -- for pos=0, GRID_SIZE-1 do
        if costHp[pos] then
            local num = costHp[pos];
            local grid = DungeonM.getGridByPos(pos + 1);
            local monster = grid.monster;
            local steam_machine = grid.steam_machine;

            if steam_machine then
                -- 如果破碎了
                if 1 == num then
                    -- 蒸汽机械变破碎了
                    grid.broken = 1;

                    EventMgr.fire(event.DEFEAT_AIRSHIP, {["class_id"] = steam_machine.classId, ["pos"] = pos, });
                end
            elseif monster then
                CombatM.doDamage(ME.user, monster, num, 0, false, {["seckill"] = true,});

                if monster:isDead() then
                    table.insert(kill_list, monster);
                end
            end
        end
    end

    -- 切换状态
    DungeonM.switchState(DUNGEON_GAMING);

    -- 抛出事件
    EventMgr.fire(event.DUNGEON_SKY_COMBAT, {
        ["result"]    = result["result"],
        ["win_times"] = result["win_times"],
        ["defenser_list"]     = result["defenser_list"],
        ["kill_monster_list"] = kill_list,
    });
end

function recoverFightAbility(rate)
    if rate <= 0 then
        return false;
    end

    local data = ME.user.dbase:query("dungeon_sc_data");
    if not data then
        return false;
    end

    if type(data["max_fight_ability"]) ~= "number" then
        return false;
    end

    data["fight_ability"] = data["fight_ability"] + FormulaM.invoke("CALC_BIG_DIVISION", rate, data["max_fight_ability"], 1000);

    data["fight_ability"] = math.min(data["fight_ability"], data["max_fight_ability"]);

    return true;
end
